package com.bdqn.service.impl;

import com.bdqn.mapper.RoleMapper;
import com.bdqn.mapper.UserMapper;
import com.bdqn.pojo.Role;
import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;

    @Override
    public User getUser(String uname, String upwd) {
        return userMapper.selectUser(uname, upwd);
    }

    @Override
    public User getUser(int uid) {
        return userMapper.selectById(uid);
    }

    @Override
    public PageInfo<User> selectPage(int pageNum, int pageSize, String name) {
        //开始分页,设置当前页的页号和每页显示的条数
        PageHelper.startPage(pageNum,pageSize);
        //获取当前页所有的用户信息信息
        List<User> list=userMapper.selectAll(name);
        //根据用户编号获取用户所有的角色的集合
        for (User user:list) {
            int uid=user.getUserId();
            List<Role> roleList=roleMapper.selectByUid(uid);
            user.setRoleList(roleList);
        }
        //获取当前页的信息对象
        PageInfo<User> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int addUser(User user) {
        return userMapper.add(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(int uid) {
        return userMapper.deleteUser(uid);
    }

    @Override
    public int addUserRole(Integer uid, List<Integer> rids) {
        //删除用户原来的角色
        userMapper.deleteRole(uid);
        //添加授予的角色
        return userMapper.addRoles(uid,rids);
    }

    @Override
    public int modifyStateClose(int uid) {
        return userMapper.modifyStateClose(uid);
    }

    @Override
    public int modifyStateOpen(int uid) {
        return userMapper.modifyStateOpen(uid);
    }


}
